﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ECCSystem.Reports
{
    public partial class frmCSRProductivityReport : Form
    {
        DataSet objDS = new DataSet();
        BizRule.ClsReports objBRReports;
        BizRule.ClsCommon objCommon;
        public frmCSRProductivityReport()
        {
            InitializeComponent();
        }

        private void frmCSRProductivityReport_Load(object sender, EventArgs e)
        {
            dtpFrom.Text = DateTime.Now.ToShortDateString();
            dtpTo.Text = DateTime.Now.ToShortDateString();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (this.CheckDate() == false)
            {
                MessageBox.Show("終了日付は開始日付より遅い。", "ECC Information");
                return;
            }
            btnCreate.Enabled = false;

            //open data source
            objBRReports = new ECCSystem.BizRule.ClsReports();
			objDS = (DataSet)objBRReports.GetCSRProductivityReportNew(dtpFrom.Value.Date, dtpTo.Value.Date);

            if (objDS.Tables[0].Rows.Count <= 0)
            {
                MessageBox.Show("データ無し。", "ECC Information");
                btnCreate.Enabled = true;
                return;
            }

            //create report
            this.CreateExcel();

            btnCreate.Enabled = true;
        }

        //Check start date less than end date
        private bool CheckDate()
        {
            if (dtpFrom.Value > dtpTo.Value)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        //excel operation(open,fill,quit)
        private void CreateExcel()
        {
            Excel.ApplicationClass objExlApp = new Excel.ApplicationClass();
            Excel.Workbook theWookbook = null;
            Excel.Worksheet theWookSheet = null;
            objCommon = new ECCSystem.BizRule.ClsCommon();
            try
            {
                objExlApp.Visible = false;

                object objMissing = System.Reflection.Missing.Value;
                StringBuilder strSavePath = new StringBuilder();
                strSavePath.Append("C:\\ECCReport\\");
                //if strSavePath is not existing,create the path
                if (Directory.Exists(strSavePath.ToString()) == false)
                {
                    Directory.CreateDirectory(strSavePath.ToString());
                }

                StringBuilder strTemplatePath = new StringBuilder();
                //Excel template path
                strTemplatePath.Append(Application.StartupPath);
                strTemplatePath.Append("\\CSRProductivity.xls");
                //if this template is existing or not
                if (File.Exists(strTemplatePath.ToString()) == false)
                {
                    MessageBox.Show("レポートのテンプレートファイルが見つかりません。", "ECC Information");
                    //quit excel object
                    objCommon.QuitExcelApp(objExlApp, theWookbook, theWookSheet, null);
                    System.GC.Collect();
                    btnCreate.Enabled = true;
                    return;
                }
                //open the excel template
                theWookbook = objExlApp.Workbooks.Open(strTemplatePath.ToString(), objMissing, objMissing, objMissing, objMissing, objMissing,
                                         objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing);
                //get the first sheet
                theWookSheet = (Excel.Worksheet)theWookbook.Worksheets[1];

                //fill data into excel
                theWookSheet.Cells[1, 2] = dtpFrom.Value.Date.ToString("yyyy/MM/dd") + "-" + dtpTo.Value.Date.ToString("yyyy/MM/dd");

                // intRow is the db row no. of the next operator start record 
                int intRow = 0;
                for (int j = 0; j < objDS.Tables[0].Rows.Count/22; j++)
                {
                    intRow = j * 22;
                    theWookSheet.Cells[j + 3, 1] = objDS.Tables[0].Rows[intRow][0].ToString();
                    theWookSheet.Cells[j + 3, 2] = objDS.Tables[0].Rows[intRow][1].ToString();
                    for (int m = 0; m < 22; m++)
                    {
                        theWookSheet.Cells[j + 3, m + 3] = float.Parse(objDS.Tables[0].Rows[intRow + m][2].ToString()).ToString("F1");
                    }
                   
                }

                //set excel report save path and name
				strSavePath.Append("CSR別時間帯効率" + dtpFrom.Value.Date.ToString("yyyy-MM-dd") + "-" + dtpTo.Value.Date.ToString("yyyy-MM-dd"));
				//strSavePath.Append(DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".xls");
				
				if (File.Exists(strSavePath.ToString() + ".xls") == true)
				{
					File.Delete(strSavePath.ToString() + ".xls");
				}

                //if the report has existed or not
                if (File.Exists(strSavePath.ToString()) == true)
                {
                    MessageBox.Show("このファイルがもう存在しました！", "ECC Information");
                    //quit excel object
                    objCommon.QuitExcelApp(objExlApp, theWookbook, theWookSheet, null);
                    System.GC.Collect();
                    btnCreate.Enabled = true;
                    return;
                }

                //save the report
                theWookbook.SaveAs(strSavePath.ToString(), objMissing, objMissing, objMissing, objMissing, objMissing,
                    Excel.XlSaveAsAccessMode.xlNoChange, objMissing, objMissing, objMissing, objMissing);
                //prompt save success
                MessageBox.Show("操作成功しました。", "ECC Information");

                //quit excel object
                objCommon.QuitExcelApp(objExlApp, theWookbook, theWookSheet, null);
                System.GC.Collect();
                this.Close();
            }
            catch (Exception ex)
            {
                //quit excel object
                objCommon.QuitExcelApp(objExlApp, theWookbook, theWookSheet, null);
                System.GC.Collect();

                //MessageBox.Show(ex.ToString(), "ECC Catch Error");
                btnCreate.Enabled = true;
            }
        }
    }
}
